Vue.component("ws", {
    props: ["url"],
    data() {
        return {
            ws: null,
            status: "closed"
        }
    },
    methods: {
        conn(id) {
            this.ws = new WebSocket("ws://" + location.host + this.url + "/" + id);
            this.ws.onmessage = (msg) => {
                this.$emit("message", msg.data);
            };
            this.ws.onopen = () => {
                this.status = "opened";
                this.$emit("open");
            };
            this.ws.onclose = () => {
                this.status = "closed";
                this.$emit("close");
            };
        },
        send(msg) {
            this.ws.send(msg);
        }
    }
})